package com.example.flex.utils.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

/**
 * @Author: 不是菜鸡爱编程
 * @Date: 2023/10/25
 * @Description：@see Serializable
 */
@ToString
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "BaseResponse", description = "后端统一返回该对象给前端，包括响应码、响应信息、响应数据")
public class BaseResponse<T> implements Serializable {
    /**
     * 响应码
     */
    @ApiModelProperty(value = "响应码")
    private Integer code;
    /**
     * 响应信息
     */
    @ApiModelProperty(value = "响应信息")
    private String message;
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public Integer getCode() {
        return code;
    }

    public BaseResponse<T> setCode(Integer code) {
        return new BaseResponse<>(code,message,data);
    }

    public String getMessage() {
        return message;
    }

    public BaseResponse<T> setMessage(String message) {
        return new BaseResponse<>(code,message,data);
    }

    public T getData() {
        return data;
    }

    public BaseResponse<T> setData(T data) {
        return new BaseResponse<>(code,message,data);
    }


    /**
     * 成功
     *
     * @return {@link BaseResponse}<{@link T}>
     */
    public static <T> BaseResponse<T> success() {
        return new BaseResponse<>(200, null, null);
    }

    /**
     * 成功
     *
     * @param message 消息
     * @return {@link BaseResponse}<{@link T}>
     */
    public static <T> BaseResponse<T> success(String message) {
        return new BaseResponse<>(200, message, null);
    }

    /**
     * 成功
     *
     * @param data    数据
     * @param message 消息
     * @return {@link BaseResponse}<{@link T}>
     */
    public static <T> BaseResponse<T> success(T data, String message) {
        return new BaseResponse<>(200, message, data);
    }


    /**
     * 错误
     *
     * @return {@link BaseResponse}<{@link T}>
     */
    public static <T> BaseResponse<T> error() {
        return new BaseResponse<>(400, null, null);
    }
    /**
     * 错误
     *
     * @param message 消息
     * @return {@link BaseResponse}<{@link T}>
     */
    public static <T> BaseResponse<T> error(String message) {
        return new BaseResponse<>(400, message, null);
    }

    /**
     * 错误
     *
     * @param data    数据
     * @param message 消息
     * @return {@link BaseResponse}<{@link T}>
     */
    public static <T> BaseResponse<T> error(T data, String message) {
        return new BaseResponse<>(400, message, data);
    }

    /**
     * 定义
     *
     * @param code    代码
     * @param data    数据
     * @param message 消息
     * @return {@link BaseResponse}<{@link T}>
     */
    public static <T> BaseResponse<T> define(Integer code, T data, String message) {
        return new BaseResponse<>(400, message, data);
    }
}
